package com.linwen.javase.学校;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Firstpage extends JFrame implements ActionListener{

	JButton hdenglu;                           
	JButton hzhuce;
	JButton gdenglu;
	JButton gzhuce;
	JLabel huiyuan;
	JLabel guanliyuan;
	JLabel zhanghao;
	JLabel mima;
	JPanel  pane;
	JPanel contentpane;
	JTextField zh;
	JTextField mm;
	JPasswordField pmm;
	int biaoji;     //标记当前面板是第几个面板
	String username;    //用来记录当前登陆的用户
	int zongzhang;      //记录购买物品的总价格
	
	JButton h1;                    //三个备用标按钮 用来设置提交、返回、购物车、订单查询 等按钮
	JButton h2;
	JButton h3;
	
	String DBDiver;                                       //数据库操作对象
    String connectionStr;
    Connection con;
    PreparedStatement stmt;
    ResultSet rs;
    
    huiyuanzhuce hyzc;                                    //各个界面的面板
    guanliyuanzhuce glyzc;
    huiyuandenglu hydl;
    gouwuche gwc;
    dingdan dd;
    guanliyuandenglu glydl;
    shangpinxiugai spxg;
    shangpinshanchu spsc;
	
	private void init(){           //实现组件的布局
		
		hdenglu=new JButton("登陆");
		hzhuce=new JButton("注册");
		gdenglu=new JButton("登陆");
		gzhuce=new JButton("注册");
		huiyuan=new JLabel("会员");
		guanliyuan=new JLabel("管理员");
		zhanghao=new JLabel("账号");
		mima=new JLabel("密码");
		pane=new JPanel();
		contentpane=new JPanel();
		zh=new JTextField();                  //zhanghao mima
		mm=new JTextField();
		pmm=new JPasswordField();
		
		h1=new JButton("dd");
		h2=new JButton("dd");
		h3=new JButton("dd");
		
		hyzc=new huiyuanzhuce();
		glyzc=new guanliyuanzhuce();
		hydl=new huiyuandenglu();
        //gwc=new gouwuche();
        //dd=new dingdan();
        glydl=new guanliyuandenglu();
        spxg=new shangpinxiugai();
		spsc=new shangpinshanchu();

				
	    
		pane.setBounds(0, 0, 1280, 600);                 //第1个面板
	    hyzc.setBounds(0, 0, 1280, 600);                 //第2个面板                   //各个界面的布局
	    glyzc.setBounds(0, 0, 1280, 600);                //第3个面板
	    hydl.setBounds(0, 0, 1280, 600);                // 第4个面板
	    //spxx.setBounds(0, 0, 1280, 600);                //5
	    //gwc.setBounds(0, 0, 1280, 600);                 //6
	    //dd.setBounds(0, 0, 1280, 600);                  //7
	    glydl.setBounds(0, 0, 1280, 600);               //8
	    spxg.setBounds(0, 0, 1280, 600);                //9
	    spsc.setBounds(0, 0, 1280, 600);                //10
	    
		pane.setLayout(null);  //自己设置布局
		pane.add(hdenglu);
		pane.add(hzhuce);
		pane.add(gdenglu);
		pane.add(gzhuce);
		pane.add(huiyuan);
		pane.add(guanliyuan);
		pane.add(zhanghao);
		pane.add(mima);
		pane.add(zh);
		//pane.add(mm);
		pane.add(pmm);
		
		
		zhanghao.setBounds(515, 200, 50, 40);
		mima.setBounds(515, 230, 50, 40);
		zh.setBounds(565, 200, 200, 30);
		mm.setBounds(565, 230, 200, 30);
		pmm.setBounds(565, 230, 200, 30);
		huiyuan.setBounds(515, 260, 50, 40);
		guanliyuan.setBounds(515, 300, 50,40);
		hdenglu.setBounds(565,260,100,40);
		hzhuce.setBounds(665,260,100,40);
		gdenglu.setBounds(565, 300,100, 40);
		gzhuce.setBounds(665, 300,100, 40);
		
		h1.setBounds(490, 600, 100, 40);
		h2.setBounds(590, 600, 100, 40);
		h3.setBounds(690, 600, 100, 40);
		
		zh.addActionListener(this);
		mm.addActionListener(this);
		hdenglu.addActionListener(this);
		hzhuce.addActionListener(this);
		gdenglu.addActionListener(this);
		gzhuce.addActionListener(this);
		
		h1.addActionListener(this);
		h2.addActionListener(this);
		h3.addActionListener(this);
		
		contentpane.setLayout(null);
        setContentPane(contentpane);
		contentpane.add(pane);
		//contentpane.add(hydl);
		biaoji=1;                                                    //登陆面板设置为1
		
		
	}
	
	
	private void dataoper() throws SQLException{                                      //数据库操作
		 DBDiver="com.mysql.jdbc.Driver";
         connectionStr="jdbc:mysql://127.0.0.1/shop?useSSL=false&&characterEncoding=UTF-8";             
         con=null;
         stmt=null;
         rs=null;
        
        try {
			Class.forName(DBDiver);
		} catch (ClassNotFoundException e) {
			System.out.println("fail");
			e.printStackTrace();
		}
        con=DriverManager.getConnection(connectionStr,"root","123456");

	}
	
	public Firstpage(){
		setTitle("购物系统");
		setBounds(300,200,1280,720);
		try {
			dataoper();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		init();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);               //设置用户在此窗体上发起 "close" 时默认执行的操作.
		//((JPanel) contentPane).setOpaque(false);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Firstpage a=new Firstpage();
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {  //相应方法
		// TODO Auto-generated method stub
		   String zh1;                          //账号
		   String  mm1;                         //密码1
		   char [] mm2;                         //密码2
		
		   
		
		if(e.getSource().equals(hdenglu)){
			zh1=zh.getText();
            mm2=pmm.getPassword();
            mm1= new String(mm2);
            username=zh1;                    //标记当前登陆的账号
            
            String sql="select * from user where user_name=? and user_password=?";
            try {
				stmt=con.prepareStatement(sql);
				stmt.setString(1, zh1);
				stmt.setInt(2, Integer.parseInt(mm1));
				rs=stmt.executeQuery();

			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				System.out.println("yichang1");
				e2.printStackTrace();
			}
            

            try {
				if(rs.next()==true){      	 //第二次调用时，已经不是指向第一行=
				contentpane.add(h1);
				contentpane.add(h2);
				contentpane.add(h3);			
				h1.setText("购物车");
				h2.setText("订单查询");
				h3.setText("返回");

				contentpane.remove(pane);
				contentpane.add(hydl);
				this.repaint();	                                              //重画
				biaoji=4;                                                     //标记当前的面板
				//sql="creat table ?";
				
				}else{
					System.out.println("账号或密码错误，请重新输入");
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				System.out.println("yichang2");
			}

		}
		if(e.getSource().equals(hzhuce)&& biaoji==1){
			

			
			contentpane.add(h1);
			//contentpane.add(h2);
			contentpane.add(h3);
			h1.setText("提交");
			h2.setText("");
			h3.setText("返回");
			contentpane.remove(pane);
			contentpane.add(hyzc);
			this.repaint();	
			biaoji=2;
		}
		if(e.getSource().equals(gdenglu)){
			zh1=zh.getText();
            mm2=pmm.getPassword();
            mm1= new String(mm2);
            username=zh1;
            String sql="select * from admin where admin_name=? and admin_password=?";
            try {
				stmt=con.prepareStatement(sql);
				stmt.setString(1, zh1);
				stmt.setInt(2, Integer.parseInt(mm1));
				rs=stmt.executeQuery();


			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				System.out.println("yichang1");
				e2.printStackTrace();
			}
            
            try {
			    if(rs.next()==true){      	 //第二次调用时，已经不是指向第一行=
			
			    contentpane.add(h1);
			    contentpane.add(h2);
			    contentpane.add(h3);
			    contentpane.remove(pane);
			    contentpane.add(glydl);
			    h1.setText("增加或修改");
			    h2.setText("删除商品");
			    h3.setText("返回");
			    this.repaint();	
			    biaoji=8;
		        }else{
		        	
			    System.out.println("账号或密码错误，请重新输入");
		   }
	     } catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
			
		}
		if(e.getSource().equals(gzhuce)){
			

			
			contentpane.add(h1);
			contentpane.add(h3);
			h1.setText("提交");
			h2.setText("");
			h3.setText("返回");
			contentpane.remove(pane);
			contentpane.add(glyzc);
			this.repaint();
			biaoji=3;
		}
		if(e.getSource().equals(h1)){

			if(e.getActionCommand().equals("提交")&&biaoji==3){       //第一个面板的按钮获取不到其他面板文本框的内容?是可以获取的
				String zh=glyzc.zhanghao1.getText();
                int mm=Integer.parseInt(glyzc.mima1.getText());
                String sql="insert into admin(admin_name,admin_password) values (?,?)";
				try {
					stmt=con.prepareStatement(sql);
					stmt.setString(1, zh);
					stmt.setInt(2, mm);
					if(stmt.executeUpdate()!=0)
						System.out.println("注册成功");
					else
						System.out.println("注册失败");
				} catch (SQLException e1) {
					System.out.println("注册失败，账号重复，请重试");
					// TODO Auto-generated catch block
					//e1.printStackTrace();
				}

                
			}
			if(e.getActionCommand().equals("提交")&&biaoji==2){                    //会员注册的提交面板
				String zhanghao=hyzc.zhanhao1.getText();
				int mima=Integer.parseInt(hyzc.mima1.getText());
				int querenmima=Integer.parseInt(hyzc.querenmima1.getText());
				String xingming=hyzc.xingming1.getText();
				int dianhua=Integer.parseInt(hyzc.dianhua1.getText());
				String dizhi=hyzc.dizhi1.getText();
				
				if(mima!=querenmima){                                            //首先判断密码与确认密码是否一致
					System.out.println("密码与确认密码不对，请重新输入");
				}
				else{
					String sql="insert into user(user_name,user_realname,user_password,user_address,user_phone) values (?,?,?,?,?)";
					try {
						stmt=con.prepareStatement(sql);
						stmt.setString(1, zhanghao);
						stmt.setString(2, xingming);
						stmt.setInt(3, mima);
						stmt.setString(4, dizhi);
						stmt.setInt(5, dianhua);
						if(stmt.executeUpdate()!=0)
							System.out.println("注册成功");
						else
							System.out.println("注册失败");
					} catch (SQLException e1) {
						System.out.println("注册失败，账号重复，请重试");
						// TODO Auto-generated catch block
						//e1.printStackTrace();
					}
				}
			}
			if(e.getActionCommand().equals("购物车")&&biaoji==4){
				contentpane.remove(hydl);
				gwc=new gouwuche();
				gwc.setBounds(0, 0, 1280, 600);
				contentpane.add(gwc);
				h1.setText("总价："+zongzhang);
				h2.setText("结账");
				this.repaint();
				biaoji=6;
			}

			if(e.getActionCommand().equals("增加或修改")&&biaoji==8){
				contentpane.remove(glydl);
				contentpane.add(spxg);
                h1.setText("增加");
                h2.setText("修改");
				this.repaint();
				biaoji=9;
			}
			if(e.getActionCommand().equals("增加")&&biaoji==9){
				String a=spxg.shangpinming1.getText();             //从面板获取信息 然后加入数据库
				String b=spxg.jiage1.getText();
				String c=spxg.jianjie1.getText();
				
				String sql="insert into good(good_name,good_price,good_introduction) values (?,?,?)";
				try {
					stmt=con.prepareStatement(sql);
					stmt.setString(1, a);
					stmt.setString(2, b);
					stmt.setString(3, c);
					if(stmt.executeUpdate()!=0){
						System.out.println("添加成功");
						contentpane.remove(glydl);
						glydl=new guanliyuandenglu();
						contentpane.add(glydl);
						glydl.setBounds(0, 0, 1280, 600);
						this.repaint();
					}
					else
						System.out.println("添加失败");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			if(e.getActionCommand().equals("删除")&&biaoji==10){
				String a=spsc.shangpinming1.getText();                   //获取输入商品的名字 根据名字删除
				String sql="delete from good where good_name =?";
				try {
					stmt=con.prepareStatement(sql);
					stmt.setString(1, a);
					if(stmt.executeUpdate()!=0)  {                //返回影响的行 ，不为0表示成功，删除之后应该重写面板
						System.out.println("删除成功");
						contentpane.remove(glydl);
						glydl=new guanliyuandenglu();
						contentpane.add(glydl);
						glydl.setBounds(0, 0, 1280, 600);
						this.repaint();
					}
					else
						System.out.println("删除失败");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}

		}
		if(e.getSource().equals(h2)){
			if(e.getActionCommand().equals("订单查询")&&biaoji==4){
				contentpane.remove(hydl);
				dd=new dingdan();
				dd.setBounds(0, 0, 1280, 600); 
				System.out.println("sss");
				contentpane.add(dd);
				contentpane.remove(h1);
				contentpane.remove(h2);
				this.repaint();
				biaoji=7;
			}
			if(e.getActionCommand().equals("删除商品")&&biaoji==8){
				contentpane.remove(glydl);
				contentpane.add(spsc);
				contentpane.remove(h2);
				h1.setText("删除");
				this.repaint();
				biaoji=10;
			}
			if(e.getActionCommand().equals("修改")&&biaoji==9){
				String a=spxg.shangpinming1.getText();
				String b=spxg.jiage1.getText();
				String c=spxg.jianjie1.getText();
				
				String sql="UPDATE good SET good_price =?, good_introduction =? WHERE good_name=?";
				try {
					stmt=con.prepareStatement(sql);
					stmt.setString(3, a);
					stmt.setString(1, b);
					stmt.setString(2, c);
					if(stmt.executeUpdate()!=0){
						System.out.println("修改成功");
					    contentpane.remove(glydl);
					    glydl=new guanliyuandenglu();
					    contentpane.add(glydl);
					    glydl.setBounds(0, 0, 1280, 600);
					    this.repaint();
					}
					else
						System.out.println("修改失败");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				
			}
			if(e.getActionCommand().equals("结账")&&biaoji==6){
		        Date d = new Date();  
		        System.out.println(d);  
		        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		        String dateNowStr = sdf.format(d);  
		        System.out.println("格式化后的日期：" + dateNowStr); 
		       if(zongzhang!=0){
		        String sql="insert into `order`(order_username,order_price,order_date) values (?,?,?)";           //为什么要加反撇号？
		        try {
					stmt=con.prepareStatement(sql);
					stmt.setString(1, username);
					System.out.println(username);
					stmt.setInt(2, zongzhang);
					System.out.println(zongzhang);
					stmt.setString(3, dateNowStr);
					if(stmt.executeUpdate()!=0){
						System.out.println("购买成功");
					}else{
						System.out.println("购买失败");
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		        sql="delete from scart where scart_username=?";
		        try {
					stmt=con.prepareStatement(sql);
					stmt.setString(1, username);
					if(stmt.executeUpdate()!=0);{
						System.out.println("成功");
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					//e1.printStackTrace();
				}
		        
		        }
		        
			}
			
		}
		if(e.getSource().equals(h3)){
			if(e.getActionCommand().equals("返回")&& (biaoji==2 || biaoji==8||biaoji==3||biaoji==4)){
		    contentpane.remove(h1);
		    contentpane.remove(h2);
		    contentpane.remove(h3);
		    contentpane.remove(hyzc);          //移除面板
		    contentpane.remove(hydl);
		    contentpane.remove(glydl);
		    contentpane.remove(glyzc);
			contentpane.add(pane);
		    this.repaint();	
			biaoji=1;
			}
			if(e.getActionCommand().equals("返回")&&(biaoji==7)){
				//contentpane.remove(gwc);
				contentpane.remove(dd);
				contentpane.add(hydl);
				contentpane.add(h1);
				contentpane.add(h2);
				h1.setText("购物车");
				h2.setText("订单查询");
				biaoji=4;
				this.repaint();
			}
			if(e.getActionCommand().equals("返回")&&(biaoji==6)){
				contentpane.remove(gwc);
				contentpane.add(hydl);
				contentpane.add(h1);
				contentpane.add(h2);
				h1.setText("购物车");
				h2.setText("订单查询");
				biaoji=4;
				this.repaint();
			}
			if(e.getActionCommand().equals("返回")&&(biaoji==9||biaoji==10)){
				contentpane.remove(spxg);
				contentpane.remove(spsc);
				contentpane.add(glydl);
				contentpane.add(h2);
				h1.setText("增加或修改");
				h2.setText("删除商品");
				biaoji=8;
				this.repaint();
			}
			

		}

		
	}
	
	class huiyuanzhuce extends JPanel implements ActionListener{

		JLabel zhanghao;
		JLabel mima;
		JLabel querenmima;
		JLabel xingming;
		JLabel dianhua;
		JLabel dizhi;
		JButton tijiao;
		JButton fanhui;
		
		JTextField  zhanhao1;
		JTextField  mima1;
		JTextField  querenmima1;
		JTextField  xingming1;
		JTextField  dianhua1;
		JTextField  dizhi1;
		
		public huiyuanzhuce(){
			init();
		}
		public void init(){
			zhanghao=new JLabel("账号");
			mima=new JLabel("密码");
			querenmima=new JLabel("确认密码");
			xingming=new JLabel("姓名");
			dianhua=new JLabel("联系电话");
			dizhi=new JLabel("地址");
			tijiao=new JButton("提交");
			fanhui=new JButton("返回");
			
		    zhanhao1=new JTextField();
		    mima1=new JTextField();
		    querenmima1=new JTextField();
	        xingming1=new JTextField();
		    dianhua1=new JTextField();
		    dizhi1=new JTextField();
		    
		    this.setLayout(null);
			
		    this.add(zhanghao);
		    this.add(mima);
		    this.add(querenmima);
		    this.add(xingming);
		    this.add(dianhua);
		    this.add(dizhi);
		   // this.add(tijiao);
		   // this.add(fanhui);
		    
		    this.add(zhanhao1);
		    this.add(mima1);
		    this.add(querenmima1);
		    this.add(xingming1);
		    this.add(dianhua1);
		    this.add(dizhi1);
			
		    
		    zhanghao.setBounds(510,200,60,30);
			mima.setBounds(510,230,60,30);
			querenmima.setBounds(510,260,60,30);
			xingming.setBounds(510,290,60,30);
			dianhua.setBounds(510,320,60,30);
			dizhi.setBounds(510,350,60,30);
			tijiao.setBounds(570,410,100,30);
			fanhui.setBounds(670,410,100,30);
			
			zhanhao1.setBounds(570,200,200,30);
			mima1.setBounds(570,230,200,30);
			querenmima1.setBounds(570,260,200,30);
			xingming1.setBounds(570,290,200,30);
			dianhua1.setBounds(570,320,200,30);
			dizhi1.setBounds(570,350,200,30);
			
			tijiao.addActionListener(this);
			//fanhui.addActionListener(this);
		    
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource().equals(tijiao)){
				
			}
			if(e.getSource().equals(fanhui)){
					contentpane.remove(hyzc);
					//contentpane.add(glyzc);
					this.repaint();	

			}
		}
		
	}
	
     class guanliyuanzhuce extends JPanel implements ActionListener{

    	 JLabel zhanghao;
    	 JLabel mima;
    	 JButton tijiao;
    	 JButton tuichu;
    	 
 		JTextField  zhanghao1;
 		JTextField  mima1;
    	 
 		public guanliyuanzhuce(){
 		  init();	
 		}
 		
 		public void init(){
 			zhanghao=new JLabel("账号");
 			mima=new JLabel("密码");	
 			tijiao=new JButton("提交");
 			tuichu=new JButton("退出");	
 			zhanghao1=new JTextField();
 			mima1=new JTextField();
 			
 			this.setLayout(null);
 			
 			this.add(zhanghao);
 			this.add(mima);
 			//this.add(tijiao);
 			//this.add(tuichu);
 			this.add(zhanghao1);
 			this.add(mima1);
 			
 			zhanghao.setBounds(510,200,60,30);
 			mima.setBounds(510,230,60,30);
 			tijiao.setBounds(570,260,100,30);
 			tuichu.setBounds(670,260,100,30);
 			zhanghao1.setBounds(570,200,200,30);
 			mima1.setBounds(570,230,200,30);
 			
 			tijiao.addActionListener(this);
 			tuichu.addActionListener(this);
 		}
 		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
            if(e.getSource().equals(tijiao)){

			}
            if(e.getSource().equals(tuichu)){

			}
		}
		
	}
	class huiyuandenglu extends JPanel implements ActionListener{
    
		JButton tuijian;
		JButton top10;
		JButton shulei;
		JButton shuiguo;
		JButton chazhao;
		JButton gouwuche; 
		JButton dingdanchaxun;
		JButton tuichu;
		
		JTextField chazhao1;
		
		JPanel contentpane;
		JLabel [] good=new JLabel[30]; //开20个标签
		JScrollPane sp;  //滚动面板类
		hydllistener hd;
		shanpingxinxi2 sx;
		int jilu;        //记录数据库里面商品的个数
		HashMap<Integer,String> ji1;           //开三个Map 分别储存 商品名，价格，简介
		HashMap<Integer,Integer> ji2;
		HashMap<Integer,String> ji3;
		int ii;         //用来标记点击时的商品代号
		int jj=0;         //标记购物的次数
		int kk;           //用来标记上一次购物时的商品的代号
		String s1;
		int s2;
		String s3;
		
		public huiyuandenglu(){
			init();
		}
		public void init(){
			tuijian=new JButton("推荐");
			top10=new JButton("top10");
			shulei=new JButton("书类");
			shuiguo=new JButton("水果");
			chazhao=new JButton("查找");
			gouwuche=new JButton("购物车");
			dingdanchaxun=new JButton("订单查询");
			tuichu=new JButton("退出");
			ji1=new HashMap<Integer,String>();
			ji2=new HashMap<Integer,Integer>();
			ji3=new HashMap<Integer,String>();
			
			
			chazhao1=new JTextField();
			contentpane=new JPanel();
			sp=new JScrollPane();
			hd=new hydllistener();
			sx=new shanpingxinxi2();
            sx.setBounds(0, 0, 720,470);
			
            sx.gouwuche.addActionListener(sx);
            sx.fanhui.addActionListener(sx);
            
            
			this.setLayout(null);
			//this.add(tuijian);
			//this.add(top10);
			//this.add(shulei);
			//this.add(shuiguo);
			this.add(chazhao);
			this.add(chazhao1);
			this.add(contentpane);
			//contentpane.add(sp);
			
			
			tuijian.setBounds(340,100,100,30);
			top10.setBounds(440,100,100,30);
			chazhao.setBounds(540,100,100,30);
			chazhao1.setBounds(640,100,100,30);
	        //chazhao.setBounds(740,100,100,30);
	        //chazhao1.setBounds(840,100,100,30);
	        contentpane.setBounds(280,130,720,470);
	        //contentpane.setBackground(Color.white);
	        
	        chazhao.addActionListener(this);
            
	        paint1();

			
		}
		public void paint1(){                    //画商品信息
			contentpane.setLayout(new GridLayout(0,3));
			String sql="select * from good";
            try {
				stmt=con.prepareStatement(sql);
				rs=stmt.executeQuery();
		        int i=0;
				while(rs.next())            //必须要判断rs结果集是否存在
		        {
		        	//System.out.println(rs.getString("admin_name")+"");
		        	//System.out.println(rs.getInt("admin_password")+"");
		        
	        	good[i]=new JLabel("商品名称"+(i+1)+" :"+rs.getString(1));
	        	good[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
	        	//contentpane.add(good[i]);
	        	good[i].addMouseListener(hd);
	             i=i+1;
	             jilu=i;
	             ji1.put(jilu, rs.getString(1));  //商品名
	             ji2.put(jilu, rs.getInt(2));     //价格
	             ji3.put(jilu, rs.getString(3));   //简介
		        }

				
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				System.out.println("yichang1");
				e2.printStackTrace();
			}
            paint2();
		}
		public void paint2(){
			for(int i=0;i<jilu;i++){
				contentpane.add(good[i]);
			}
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource().equals(chazhao)){
				
				s1=chazhao1.getText();
				
				contentpane.removeAll();
				String sql="SELECT * FROM good WHERE good_name=?";
				
				try {
					stmt=con.prepareStatement(sql);
					stmt.setString(1, s1);
					rs=stmt.executeQuery();
					System.out.println(s1);
					while(rs.next()){
						sx.shangpinming.setText("商品名："+s1);
						System.out.println(s1);
						sx.jiage.setText("价格："+rs.getInt(2)+"块");
						sx.jianjie.setText("简介："+rs.getString(3));
		                contentpane.add(sx);
		                contentpane.repaint();
                        ii=getkey(s1);         //根据values得到键
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				

			}
			
		}
		public int getkey(String a){
			int b;
			int key=0;
			Set keys=ji1.keySet(); //返回key的集合
			Iterator it = keys.iterator();
            while(it.hasNext()){
                b=(int) it.next(); 
            	if(ji1.get(b).equals(a)){
            	   key=b;
               }
            }
		    return key;
		}
		
		  class hydllistener extends MouseAdapter{

			  
			  public void mouseClicked(MouseEvent e) {
                System.out.println("woshi");
                contentpane.removeAll();
                JLabel label1= (JLabel) e.getSource();
                String label2=label1.getText();
                label2=label2.substring(4, 6);
                char b=label2.charAt(1);
                int c=b;
                System.out.println(c+"dd");
               
                if(c==32){
                	label2=label1.getText();
                	label2=label2.substring(4, 5);
                	ii = Integer.parseInt(label2);
                }
                else{
                	label2=label1.getText();
                	label2=label2.substring(4, 6);
                    ii = Integer.parseInt(label2);
              }
                sx.shangpinming.setText("商品名："+ji1.get(ii));
                sx.jiage.setText("价格："+ji2.get(ii)+"块");
                sx.jianjie.setText("简介："+ji3.get(ii));
                contentpane.add(sx);
                contentpane.repaint();
			  }

		  }
			class shanpingxinxi2 extends JPanel implements ActionListener{                  //点击切换到另一个面板 显示商品的信息

				JLabel shangpinming;
				JLabel jiage;
				JLabel jianjie;
				JButton gouwuche;
				JButton fanhui;
				
				JTextField shangpin1;
				JTextField jiage1;
				JTextField jianjie1;
				
				public shanpingxinxi2(){
					init();
				}
				public void init(){                                          
					shangpinming=new JLabel("商品名");
					jiage=new JLabel("价格");
					jianjie=new JLabel("简介");
					gouwuche=new JButton("购物");
					fanhui=new JButton("返回");

					
					this.setLayout(null);
					this.add(shangpinming);
					this.add(jiage);
					this.add(jianjie);
					this.add(gouwuche);
					this.add(fanhui);

					
					shangpinming.setBounds(160,100,200,30);
					jiage.setBounds(160,130,200,30);
					jianjie.setBounds(160,160,200,30);
					gouwuche.setBounds(260,390,100,30);
					fanhui.setBounds(360,390,100,30);
					
					
				}
				@Override
				public void actionPerformed(ActionEvent e) {        //为什么画不出来
					// TODO Auto-generated method stub
					if(e.getSource().equals(fanhui)){
						contentpane.remove(sx);
						paint2();
						contentpane.repaint();
						System.out.println("wuwuwuwuwuwu");
					}
					if(e.getSource().equals(gouwuche)){               //每次都以用户名新建一个购物车表，
					
						System.out.println(ii);
						
						s1=ji1.get(ii);                       //名字
						s2=ji2.get(ii);                          //jiage
						s3=ji3.get(ii);                       //简介
					
						
						if(kk==ii){
						jj=jj+1;
						System.out.println(kk);
						System.out.println(jj);
						}
						else{
						   jj=1;
						   kk=ii;
							System.out.println(kk);
							System.out.println(jj);
						}
						if(jj==1){
						String sql="insert into  scart(scart_goodnumber,scart_username,scart_price,scart_good) values (?,?,?,?)";
						try {
							stmt=con.prepareStatement(sql);
							stmt.setString(4, s1);
							stmt.setString(2, username);   //当前登陆的用户
							stmt.setInt(3, s2);
							stmt.setInt(1, jj);
							if(stmt.executeUpdate()!=0){
								System.out.println("购物成功");
							}else{
								System.out.println("购物失败");
							}
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						}else{
						String sql="update scart set scart_goodnumber=? where scart_username=? and scart_good=?";
						try {
							stmt=con.prepareStatement(sql);
							stmt.setString(2, username);
							stmt.setInt(1, jj);
							stmt.setString(3, s1);
							if(stmt.executeUpdate()!=0){
								System.out.println("购物成功");
							}else{
								System.out.println("购物失败");
							}
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						}
					}
				}
				

				
			}
	}

	class gouwuche extends JPanel{

       JLabel[] sc=new JLabel[40];
       JPanel contentpane1;
       JLabel sb;
       
       public gouwuche(){
    	   init();
       }
		
		public void init(){
			System.out.println("woshiwwwwwww");
			contentpane1=new JPanel();
			this.setLayout(null);
			this.add(contentpane1);
			contentpane1.setBounds(280,130,720,470);
			contentpane1.setBorder(BorderFactory.createLineBorder(Color.black));
			//contentpane.setLayout(new GridLayout(0,1));           //设置布局管理         布局管理出了问题
			String sql="select scart_good,scart_price,scart_goodnumber from scart where scart_username=?";       //原来是获取不到当前用户
            try {
            	contentpane1.setLayout(new GridLayout(10,1));                                     //为什么布局管理不行？
            	stmt=con.prepareStatement(sql);
	            stmt.setString(1, username);
	            rs=stmt.executeQuery();
	            int i=0;
	            zongzhang=0;
	            while(rs.next()){
	            	sc[i]=new JLabel("商品名称："+rs.getString(1)+"价格："+rs.getInt(2)+"数量："+rs.getInt(3));
	            	System.out.println("商品名称："+rs.getString(1)+"价格："+rs.getInt(2)+"数量："+rs.getInt(3));
	            	sc[i].setBorder(BorderFactory.createLineBorder(Color.black));
	            	zongzhang=rs.getInt(2)*rs.getInt(3)+zongzhang;
	            	//contentpane1.add(sc[i]);
	            	//sc[i].setBounds(0, 0, 720, 50);
	            	i=i+1;
	            	System.out.println(i);
	            	
	            }
	            int k=0;
	            for(int j=0;j<i;j++){
	            	contentpane1.add(sc[j]);
	            	sc[j].setBounds(0,k,720,50);
	            	k=k+50;
	            }
	            System.out.println(username);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
	}
	
	
	class dingdan extends JPanel {
		
		JLabel dingdanhao;
		JLabel dingdanriqi;
		JLabel huiyuanmingcheng;
		JLabel []orders=new JLabel[30];
		
		
		public dingdan(){
			init();
		}


		public void init(){
			dingdanhao=new JLabel("订单价格");
			dingdanriqi=new JLabel("订单日期");
			huiyuanmingcheng=new JLabel("会员名称");
			this.setLayout(null);
			
			this.add(dingdanhao);
			this.add(dingdanriqi);
			this.add(huiyuanmingcheng);
			
			dingdanhao.setBounds(340, 100, 200, 30);
			dingdanriqi.setBounds(540, 100, 200, 30);
			huiyuanmingcheng.setBounds(740, 100, 200, 30);
			dingdanhao.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			dingdanriqi.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			huiyuanmingcheng.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			
			int i=0;
			int j=130;
			String sql="select * from `order` where order_username=?";
			try {
				stmt=con.prepareStatement(sql);
				stmt.setString(1, username);
				rs=stmt.executeQuery();
				while(rs.next()){
					orders[i]=new JLabel(rs.getString(2)+"");
					orders[i].setBounds(340, j, 200, 30);
					orders[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
					this.add(orders[i]);
					i=i+1;
					orders[i]=new JLabel(rs.getString(3)+"");
					orders[i].setBounds(540, j, 200, 30);
					orders[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
					this.add(orders[i]);
					i=i+1;
					orders[i]=new JLabel(rs.getString(1)+"");
					orders[i].setBounds(740, j, 200, 30);
					orders[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
					this.add(orders[i]);
					j=j+30;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	class guanliyuandenglu extends JPanel {

		JButton tuijian;
		JButton shulei;
		JButton shuiguo;
		JButton zengjiashangping;
		JLabel[] good=new JLabel[40];
		JPanel contentpane;
		HashMap<Integer, String> hm;
		
		public guanliyuandenglu(){
			init();
		}
		
		public void init(){
			
			tuijian=new JButton("推荐");
			shulei=new JButton("书类");
			shuiguo=new JButton("水果");
			zengjiashangping=new JButton("增加商品");
			contentpane=new JPanel();
			hm=new HashMap<Integer,String>();
			
			this.setLayout(null);
			//this.add(tuijian);
			//this.add(shulei);
			//this.add(shuiguo);
			//this.add(zengjiashangping);
			this.add(contentpane);
		
			
			tuijian.setBounds(440,100,100,30);
			shulei.setBounds(540,100,100,30);
			shuiguo.setBounds(640,100,100,30);
			zengjiashangping.setBounds(740,100,100,30);
			contentpane.setBounds(280,130,720,470);
			contentpane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			
			contentpane.setLayout(new GridLayout(0,1));
			
            paint1();
			
		}
		

		

	  public void paint1(){                                         //获取商品 写到管理员面板上
			int i=0;
			int j=1;        //标记 一获取标签里的内容
		       String sql="select * from good"; 
	            try {
					stmt=con.prepareStatement(sql);
					rs=stmt.executeQuery();
			        int h=1;
					while(rs.next())            //必须要判断rs结果集是否存在
			        {
			        	good[i]=new JLabel(h+rs.getString(1)+":"+rs.getInt(2)+":"+rs.getString(3));
			        	good[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
			        	contentpane.add(good[i]);
			        	hm.put(j, rs.getString(1));
			        	i=i+1;
			        	j=j+1;
			        	h=h+1;
			        }

				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					System.out.println("yichang1");
					e2.printStackTrace();
				}
	  }

		
	}
	class shangpinxiugai extends JPanel {

		JLabel shangpinming;
		JLabel jiage;
		JLabel jianjie;
		JButton tijiao;
		JButton fanhui;
		
		JTextField shangpinming1;
		JTextField jiage1;
		JTextField jianjie1;
		
		public shangpinxiugai(){
			init();
		}
		
		public void init(){
			shangpinming=new JLabel("商品名");
			jiage=new JLabel("价格");
			jianjie=new JLabel("简介");
			tijiao=new JButton("提交");
			fanhui=new JButton("返回");
			
			shangpinming1=new JTextField();
			jiage1=new JTextField();
			jianjie1=new JTextField();
			
			this.setLayout(null);
			this.add(shangpinming);
			this.add(jiage);
			this.add(jianjie);
			//this.add(tijiao);
			//this.add(fanhui);
			this.add(shangpinming1);
			this.add(jiage1);
			this.add(jianjie1);
			
			shangpinming.setBounds(510,200,60,30);
			jiage.setBounds(510,230,60,30);
			jianjie.setBounds(510,260,60,30);
			tijiao.setBounds(570,290,100,30);
			fanhui.setBounds(670,290,100,30);
			shangpinming1.setBounds(570,200,200,30);
			jiage1.setBounds(570,230,200,30);
			jianjie1.setBounds(570,260,200,30);
		}

		
	}
	class shangpinshanchu extends JPanel{                                   //删除面板
		JLabel shangpinming;
		JTextField shangpinming1;
		
		public shangpinshanchu(){
			init();
		}
		public void init(){
			shangpinming=new JLabel("商品名称");
			shangpinming.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			shangpinming1=new JTextField();
			this.setLayout(null);
			this.add(shangpinming);
			this.add(shangpinming1);
			
			shangpinming.setBounds(540, 200, 200, 40);
			shangpinming1.setBounds(540, 240, 200, 40);
			
			
		}
	}


}
